//
//  DDZdtjDetailContentVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/8/15.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDZdtjDetailContentVC.h"
#import "DDZdtjCollectionViewCell.h"
#import "DDTJ.h"
#import "UIImageView+WebCache.h"
#import "DDNetWorkAPIClient.h"
#import "EGOImageView.h"
@interface DDZdtjDetailContentVC ()
@property (strong, nonatomic)NSMutableArray *dataMArr;// 数据源
@property (strong, nonatomic)NSMutableArray *datas;
@property (weak, nonatomic) IBOutlet UICollectionView *myConllection;// collectionView
@end

@implementation DDZdtjDetailContentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpCollection];
    
    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.myConllection registerNib:cellNib forCellWithReuseIdentifier:@"IND_cvCell"];
    
    
    NSMutableArray *arr = [NSMutableArray array];
    for (DDTJCell *tj  in _dataMArr) {
        [arr addObject:tj.uid];
    }
    
    _datas = [NSMutableArray array];
    __weak DDZdtjDetailContentVC *weakSelf = self;
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network avatarGet:arr size:nil completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        if (isSuccess) {
            if (dict.count > 0) {
                NSArray *d = (NSArray *)dict;
                _datas = [NSMutableArray arrayWithArray:d];
                 [weakSelf.myConllection reloadData];
//                NSDictionary *userDict = (NSDictionary *)d[0];
//                if (userDict.count > 0) {
//                    NSString *url = [userDict objectForKey:@"url"];
//                    [imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"首页列表头像-通用"]options:SDWebImageRefreshCached];
//                    imageV.contentMode = UIViewContentModeScaleAspectFit;
//                    [cell.contentView addSubview:imageV];
//                    
//                }
            }
        }
        
    }];
}

-(void)setUpCollection{
    self.dataMArr = [NSMutableArray arrayWithArray:_array];
}
#pragma mark - Collection View Data Source
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_dataMArr.count == 0) {
        return 0;
    }
    return self.datas.count;
}

//定义每个UICollectionView 的大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(([UIScreen mainScreen].bounds.size.width-50)/3, 126);
//}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"IND_cvCell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    
    DDTJCell *tjCell = [_dataMArr objectAtIndex:indexPath.row];
    NSDictionary *dict = [_datas objectAtIndex:indexPath.row];
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    

//    [cell viewWithTag:10].hidden = YES;
//    [cell viewWithTag:11].hidden = YES;
//    [cell viewWithTag:12].hidden = YES;
//    [cell viewWithTag:188].hidden = YES;
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
    //[imageV setImageWithURL:[NSURL URLWithString:info.imgPath]]
    
    //NSString *url  = [NSString stringWithFormat:@"http://dd5s.oss-cn-shenzhen.aliyuncs.com/upload/head/%@/m.jpg?%@",tjCell.uid,tjCell.answerTime];
    NSString *url = [dict objectForKey:@"url"];
    if (url&&![url isMemberOfClass:[NSNull class]]) {
         [imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"首页列表头像-通用"]options:SDWebImageRefreshCached];
    }else{
        imageV.image = [UIImage imageNamed:@"首页列表头像-通用"];
    }
    
    
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    [cell.contentView addSubview:imageV];
    

    UILabel *labName = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 100, 20)];
    labName.text = tjCell.name;
    labName.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:labName];
//    UILabel *labPrice = (UILabel *)[cell viewWithTag:12];
//    labPrice.text = info.price;
    return cell;
};


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
